package com.meihua.shellapp.dialog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.ArraySet;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.TypeAdapter;
import com.meihua.shellapp.utils.ViewUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 作者：lc on 2017/9/27.
 * 邮箱：lc0607live@163.com
 */

public abstract class OrderScreenDialog extends Dialog implements View.OnClickListener {

    private GridView gv_type;//保险类型
    private TextView tv_reset;//重置
    private TextView tv_sure;//确定
    private RelativeLayout rl_start_time;//开始时间选择
    private RelativeLayout rl_end_time;//结束时间选择
    private TextView tv_start_time;//开始时间
    private TextView tv_end_time;//结束时间
    private EditText et_policy_holder;//投保人姓名
    private EditText et_recognizee;//被保人姓名
    private EditText et_order_num;//订单号
    private ScrollView sv;//scrollview

    private List<String> types = new ArrayList<>();
    private Calendar calendar;

    public OrderScreenDialog(@NonNull Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.dialog_order_screen);
        calendar = Calendar.getInstance();
        initView();
    }

    private void initView() {
        sv = findViewById(R.id.sv);
        gv_type = findViewById(R.id.gv_type);
        tv_reset = findViewById(R.id.tv_reset);
        tv_sure = findViewById(R.id.tv_sure);
        rl_start_time = findViewById(R.id.rl_start_time);
        rl_end_time = findViewById(R.id.rl_end_time);
        tv_start_time = findViewById(R.id.tv_start_time);
        tv_end_time = findViewById(R.id.tv_end_time);
        et_policy_holder = findViewById(R.id.et_policy_holder);
        et_recognizee = findViewById(R.id.et_recognizee);
        et_order_num = findViewById(R.id.et_order_num);
        tv_end_time = findViewById(R.id.tv_end_time);
        tv_end_time = findViewById(R.id.tv_end_time);
        sv.smoothScrollTo(0, 20);
        tv_reset.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
        rl_start_time.setOnClickListener(this);
        rl_end_time.setOnClickListener(this);

        bindData();
    }

    private void bindData() {
        et_policy_holder.setText("");
        et_recognizee.setText("");
        et_order_num.setText("");
        tv_start_time.setText("开始日期");
        tv_end_time.setText("结束日期");
        types.clear();
        types.add("车险");
        types.add("寿险");
        types.add("其他保险");
        gv_type.setAdapter(new TypeAdapter(getContext(), types));
        ViewUtils.getInstance().setHeightByGridView(gv_type);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sure://确定
                forResult(tv_start_time.getText().toString(), tv_end_time.getText().toString()
                        , et_policy_holder.getText().toString(), et_recognizee.getText().toString()
                        , et_order_num.getText().toString()
                        , ((TypeAdapter) gv_type.getAdapter()).getIndex());
                dismiss();
                break;
            case R.id.tv_reset://重置
                bindData();
                break;
            case R.id.rl_start_time://开始时间
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        // TODO Auto-generated method stub
                        //更新EditText控件日期 小于10加0
                        tv_start_time.setText(year + "-" + (month + 1) + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
            case R.id.rl_end_time://结束时间
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        // TODO Auto-generated method stub
                        //更新EditText控件日期 小于10加0
                        tv_end_time.setText(year + "-" + (month + 1) + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
        }
    }

    /**
     * 确定回调方法
     *
     * @param startTimeStr  开始时间
     * @param endTimeStr    结束时间
     * @param policy_holder 投保人姓名
     * @param recognizee    被保人姓名
     * @param order_num     订单号
     * @param typeIndex     类型下标
     */
    public abstract void forResult(String startTimeStr, String endTimeStr, String policy_holder
            , String recognizee, String order_num, int typeIndex);
}
